package com.bit.inmap;


import java.util.Calendar;

import android.os.Bundle;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Color;
import android.telephony.PhoneNumberUtils;
import android.telephony.TelephonyManager;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Toast;

public class AddMemberActivity extends Activity implements OnClickListener,OnDateSetListener{
	DatePickerDialog dateDialog;
	Button submitBtn;
	Button idCheckBtn;
	
	EditText edName, edId, edPwd, edPwdCh, edPhone,edEmail;
	Button birthBtn;

	MySQLHandler handler;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_add_member);
		
		handler = MySQLHandler.open(getApplicationContext());
		
		// 자신의 핸드폰 번호 얻어오기
		getPhoneNumber(); 
		
		// 이름
		edName = (EditText) findViewById(R.id.name_edit);
		
		// 아이디
		edId = (EditText) findViewById(R.id.id_edit);
		
		// 비밀번호
		edPwd = (EditText) findViewById(R.id.pwd1_edit);
		edPwdCh =(EditText) findViewById(R.id.pwd2_edit);	// 패스워드 확인
		
		// 이메일
		edEmail = (EditText) this.findViewById(R.id.email_edit);
		
		// 생년월일
		birthBtn = (Button) this.findViewById(R.id.birth_btn);
		birthBtn.setOnClickListener(this);
		birthBtn.setInputType(0);
		
		
		
		//키보다 안올라오게 하기
//		InputMethodManager inputManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
//		inputManager.hideSoftInputFromWindow(birth_edit.getWindowToken(), 0);
//		birth_edit.setSingleLine(false);
			
		
		
		submitBtn = (Button) this.findViewById(R.id.summit_btn);
		submitBtn.setOnClickListener(this);
		
		idCheckBtn = (Button) this.findViewById(R.id.id_ckeck);
		idCheckBtn.setOnClickListener(this);
	}



	private void getPhoneNumber() {
		// 전화번호 자동 입력
		TelephonyManager systemService = (TelephonyManager)getSystemService    (Context.TELEPHONY_SERVICE);

		String phoneNumber = systemService.getLine1Number();
		phoneNumber = phoneNumber.substring(phoneNumber.length()-10,phoneNumber.length());
		phoneNumber="0"+phoneNumber;
		phoneNumber = PhoneNumberUtils.formatNumber(phoneNumber);
		edPhone = (EditText) this.findViewById(R.id.phone_edit);
		edPhone.setText(phoneNumber);
		edPhone.setTextColor(Color.BLACK);
		edPhone.setEnabled(false);
	}

	

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.add_member, menu);
		return true;
	}

	@Override
	public void onClick(View v) {		
		switch(v.getId()){
			case R.id.birth_btn:
				Calendar cd = Calendar.getInstance();
				dateDialog = new DatePickerDialog(this, this, 
						cd.get(Calendar.YEAR), 
						cd.get(Calendar.MONTH),
						cd.get(Calendar.DAY_OF_MONTH));
				dateDialog.show();
				break;
				
			case R.id.summit_btn:
				String name  = edName.getText().toString();	 // 이름				
				String id = edId.getText().toString();		// 아이디				
				String pwd = edPwd.getText().toString();	// 패스워드
				String pwd2 = edPwdCh.getText().toString();	// 패스워드
				String phoneNum = edPhone.getText().toString();	//전화번호				
				String email = edEmail.getText().toString();	// 이메일				
				String birth = birthBtn.getText().toString();	// 생년월일
				
				// DB 정보 얻어오기
				Cursor c = handler.selectAll();
				String r_id ="";
				String chId ="";
				while(c.moveToNext()){
					r_id = c.getString(c.getColumnIndex("id"));
					if(id.equals(r_id)){
						chId = r_id;
					}	
				}			
								
				if(c != null){
					c.moveToFirst();
				}
				
				if(name.isEmpty() || id.isEmpty() || pwd.isEmpty() || pwd2.isEmpty()){
					Toast.makeText(this, "빈칸을 채워주세요", Toast.LENGTH_SHORT).show();
				}else if(!pwd.equals(pwd2)){
					Toast.makeText(this, "정확한 패스워드를 입력해주세요", Toast.LENGTH_SHORT).show();
				}else if(id.equals(chId)){
					Toast.makeText(this, "중복된 아이디가 있습니다", Toast.LENGTH_SHORT).show();
					EditText eId = (EditText) this.findViewById(R.id.id_edit);
					eId.setText("");
				}else{
					
					// DB 에 넣기
					handler.insert(name, id, pwd, phoneNum, email, birth);
					handler.close();				
					Toast toast = Toast.makeText(this, "회원가입 성공", Toast.LENGTH_SHORT);
					toast.setGravity(Gravity.CENTER, 0, 0);
					toast.show();
					MainActivity.login_check = true;
					if(MainActivity.login_check)	{
						MainActivity.loginBtn.setBackgroundResource(R.drawable.logout_c);
					}else{
						MainActivity.loginBtn.setBackgroundResource(R.drawable.login_c);
					}
					Intent intent = new Intent(this, LoginInfoActivity.class);
					startActivity(intent);
				}

				break;
				
			case R.id.id_ckeck:
				edId = (EditText) findViewById(R.id.id_edit);
				id = edId.getText().toString();
				
				c = handler.selectAll();
				if(c != null){
					c.moveToFirst();
				}
				
				String r_id1 ="";
				String chId2 ="";
				while(c.moveToNext()){
					r_id1 = c.getString(c.getColumnIndex("id"));
					if(id.equals(r_id1)){
						chId2 = r_id1;
					}		
				}
				if(id.isEmpty()){
					Toast.makeText(this, "아이디를 작성해주세요", Toast.LENGTH_SHORT).show();
				}else if(id.equals(chId2)){
					Toast.makeText(this, "중복된 아이디가 있습니다", Toast.LENGTH_SHORT).show();
					EditText eId = (EditText) this.findViewById(R.id.id_edit);
					eId.setText("");
				}else{
					Toast.makeText(this, "중복된 아이디가 없습니다", Toast.LENGTH_SHORT).show();
				}
				handler.close();
				break;
		}
	}
	

	@Override
	public void onDateSet(DatePicker view, int year, int monthOfYear, int dateOfMonth) {
		// TODO Auto-generated method stub
		String str = year+"년 "+(monthOfYear+1)+"월 "+dateOfMonth+"일 ";
		birthBtn.setText(str);
	}

}
